import org.apache.ibatis.executor.statement.RoutingStatementHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;

import java.sql.Statement;
import java.util.Properties;

/**
 * @author cxx
 * @create 2020-05-29 18:10
 **/
//@Intercepts({
//        @Signature(type = StatementHandler.class,method = "query",args = {Statement.class, ResultHandler.class}),
//        @Signature(type = StatementHandler.class,method = "update",args = {Statement.class})
//})
public class RouteInterceptor implements Interceptor {


    private Properties properties;

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object proceed = invocation.proceed ();
        RouteDataSource.removeRoute ();
        return proceed;
    }

    @Override
    public Object plugin(Object target) {

        if(target instanceof RoutingStatementHandler){
//            ((RoutingStatementHandler) target).getBoundSql ().
            RouteDataSource.addRoute ("ds2");
        }

        return target;
    }

    @Override
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}
